﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
namespace EzFrameWork
{
    [Serializable]
    public class EGODM
    {
        public List<Todo> ToggleList;
        public EGODM()
        {
            ToggleList = new List<Todo>();
        }

        public List<string> GetListString()
        {
            List<string> temp = new List<string>();
            foreach (var item in ToggleList)
            {
                temp.Add(item.Content);
            }
            return temp;
        }
    }
    [Serializable]
    public class Todo
    {
        public string Content;
        public bool Finished;

        public Action OnFinishedChanged;
        public bool FinishedValue
        {
            get { return Finished; }
            set
            {
                if (value != Finished)
                {
                    Finished = value;
                    OnFinishedChanged();
                }
            }
        }


    }
    public class MainWindow : EditorWindow
    {

        bool isShowMenuItem = false;
        [MenuItem("EGO/MainWindow %#t")]
        static void Open()
        {
            var window = GetWindow<MainWindow>(true);
            window.position = new Rect(new Vector2(1000, 500), new Vector2(400, 500));
            if (!window.isShowMenuItem)
            {
                var todoContent = EditorPrefs.GetString("EGO_TODOS", string.Empty);
                if (string.IsNullOrEmpty(todoContent))
                {

                }
                else
                {
                    window.ToggleList = JsonUtility.FromJson<EGODM>(todoContent);
                }
                window.Show();
                window.isShowMenuItem = true;
            }
            else
            {
                window.Close();
                window.isShowMenuItem = false;
            }
        }


        bool toogleTest;
        // List<string> ToggleList = new List<string>();
        EGODM ToggleList = new EGODM();
        private string mInpuntContent = string.Empty;

        private void OnGUI()
        {

            GUILayout.BeginVertical(style: "box");
            mInpuntContent = EditorGUILayout.TextField(mInpuntContent);
            if (GUILayout.Button("添加"))
            {
                if (mInpuntContent != "")
                {
                    Todo todo = new Todo();
                    todo.Content = mInpuntContent;
                    todo.Finished = false;
                    ToggleList.ToggleList.Add(todo);
                    mInpuntContent = string.Empty;
                    SaveData();
                }
            }

            for (int i = ToggleList.ToggleList.Count - 1; i > 0; i--)
            {
                CreateToggle(ToggleList.ToggleList[i]);
            }

            GUILayout.EndVertical();
        }


        void CreateToggle(Todo todo)
        {
            GUILayout.BeginHorizontal();
            todo.FinishedValue = GUILayout.Toggle(todo.Finished, todo.Content);
            todo.OnFinishedChanged = () =>
            {
                Debug.Log("gaibian");
                // if (todo.FinishedValue)
                // {

                // }
                // else
                // {
                //     if (!todo.Content.Contains(";已经完成"))
                //     {
                //         todo.Content = todo.Content + ";已经完成";
                //     }
                // }
                SaveData();
            };
            if (GUILayout.Button("删除"))
            {
                ToggleList.ToggleList.Remove(todo);
                SaveData();
            }
            GUILayout.EndHorizontal();

        }

        void SaveData()
        {
            EGODM o = new EGODM();
            o.ToggleList = ToggleList.ToggleList;
            string data = JsonUtility.ToJson(o);
            EditorPrefs.SetString("EGO_TODOS", data);
        }
    }
}

